<?php

/**
 * This is the model class for table "sourcemessage".
 *
 * The followings are the available columns in table 'sourcemessage':
 * @property integer $id
 * @property string $category
 * @property string $message
 *
 * The followings are the available model relations:
 * @property Message[] $messages
 */
class SourceMessage extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return SourceMessage the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sourcemessage';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category', 'length', 'max'=>32),
			array('message', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, category, message', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'messages' => array(self::HAS_MANY, 'Message', 'id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'category' => 'Category',
			'message' => 'Message',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('category',$this->category,true);
		$criteria->compare('message',$this->message,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	/**
	 * manage DB translation by categorie, key and array of translations
	 * @param String $category
	 * @param String $key
	 * @param Array $translations array('fr'=>'translation in fr'), key is the Lang and Value the translation
	 */
	public static function manageTranslation($category,$key,$translations){
		//Gestion de al source, on pourrait rajouter tests sur category et key ...
		$sourceMessage=SourceMessage::model()->find('category=:category AND message=:key',array(':category'=>$category,':key'=>$key));
		if (empty($sourceMessage)){
			$sourceMessage = new SourceMessage();
			$sourceMessage->category=$category;
			$sourceMessage->message=$key;
			$sourceMessage->save();
		}
		//Si des translations sont fournies
		if(!empty($translations)){
			foreach (array_keys($translations) as $translationKey){
				$message=Message::model()->find('id=:id and language=:lang',array(':id'=>$sourceMessage->id,':lang'=>$translationKey));
				if (empty($message)){
					$message = new Message();
					$message->id=$sourceMessage->id;
					$message->language=$translationKey;
					$message->translation=$translations[$translationKey];
					$message->save();
				}
				else{
					$message->translation=$translations[$translationKey];
					$message->update();
				}
			}
		}
	}	
	
	/**
	 * delete DB translation by categorie, key and array of translations
	 * @param String $category
	 * @param String $key
	 * @param Array $translations array('fr'=>'translation in fr'), key is the Lang and Value the translation
	 */
	public static function deleteTranslation($category,$key){
		
		//suppression source
		$id_message=SourceMessage::model()->deleteAll('category=:category and message=:key',array(':category'=>$category,':key'=>$key,));
		
		//suppression message
		Message::model()->deleteAll('id=:id_message',array(':id_message'=>$id_message));
					
		
	}	
	
}